package com.kingdee.pda.plugins.http.serverinterface.impl;

import android.text.TextUtils;

import com.kingdee.pda.entity.WarehouseLocation;
import com.example.basecommon.utils.LoginUtil;
import com.kingdee.pda.plugins.http.common.KeyConstants;
import com.kingdee.pda.plugins.http.common.OnRequestCallback;
import com.kingdee.pda.plugins.http.serverinterface.IQueryGoodsLocationStock;
import com.kingdee.pda.util.CommonUtil;
import com.example.basecommon.utils.ConstantUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by zhanlg
 * DATE 2021/5/28
 * TIME 9:08
 */
public class QueryGoodsLocationStockImpl implements IQueryGoodsLocationStock {

    private static final String API_NAME = "/kapi/app/oewms/query_location_stock_pda";


    @Override
    public void queryGoodsLocationStockData(final WarehouseLocation moveLocation, long skuId, int isDefaultLocation, final OnRequestCallback<WarehouseLocation> callback) {
        String apiUrl = ConstantUtil.getServerUrl() + API_NAME;
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"),
                setQueryParamValue(moveLocation.getWarehouseId(), moveLocation.getLocationId(), skuId, isDefaultLocation).toString());

        Request request = new Request.Builder()
                .addHeader("accessToken", LoginUtil.getToken())
                .url(apiUrl)
                .post(requestBody)
                .build();
        OkHttpUtils.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                callback.onFailedCallback(false, CommonUtil.getErrorMsg(e.getMessage(), "请求失败！"));
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.code() == 200) {
                    String result = response.body().string();
                    if (!TextUtils.isEmpty(result) && result.contains(KeyConstants.Response.SUCCESS)) {
                        JSONObject resultJson;
                        try{
                            resultJson = new JSONObject(result);
                            boolean isSuccess = resultJson.optBoolean(KeyConstants.Response.SUCCESS, false);
                            if (isSuccess) {
                                JSONObject data = resultJson.getJSONObject(KeyConstants.Response.DATA);
                                if (data != null) {
                                    moveLocation.setWarehouseId(data.optLong("warehouseId"));
                                    moveLocation.setWarehouseNum(data.optString("warehouseCode"));
                                    moveLocation.setLocationId(data.optLong("locationId"));
                                    moveLocation.setLocationCode(data.optString("locationCode"));
                                    moveLocation.setLocationName(data.optString("locationName"));
                                    moveLocation.setSkuId(data.optLong("skuId"));
                                    moveLocation.setSkuCode(data.optString("skuCode"));
                                    moveLocation.setSkuName(data.optString("skuName"));
//                                    moveLocation.setStockQty(data.optInt("stockQty"));
//                                    moveLocation.setAvailableQty(data.optInt("availableQty"));
//                                    moveLocation.setWaitMoveQty(data.optInt("waitMoveQty"));
                                    callback.onSuccessCallback(moveLocation, true);
                                } else {
                                    callback.onFailedCallback(false, "数据返回异常");
                                }
                            } else {
                                callback.onFailedCallback(false,
                                        CommonUtil.getErrorMsg(resultJson.optString(KeyConstants.Response.MESSAGE), "请求失败！"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            callback.onFailedCallback(false, CommonUtil.getErrorMsg(response.message(), "数据解析失败！"));
                        }
                    } else {
                        callback.onFailedCallback(true, CommonUtil.getErrorMsg(response.message(), "请求失败！"));
                    }
                } else {
                    callback.onFailedCallback(false, response.message());
                }
            }
        });
    }

    private JSONObject setQueryParamValue(long warehouseId, long locationId, long skuId, int isDefaultLocation) {
        JSONObject json = new JSONObject();
        try {
            json.put("warehouseId", warehouseId);
            json.put("locationId", locationId);
            json.put("skuId", skuId);
            json.put("defaultLocation", isDefaultLocation);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json;
    }
}
